<?php

namespace app\controller;


use app\service\UserSpaceTrainService;
use support\Response;
use think\Exception;
use think\facade\Db;

//用户宇宙列车控制器
class UserSpaceTrainController extends Base
{

    /**
     * 获取宇宙列车信息
     */
    public function index(): Response
    {
        // 启动事务
        Db::startTrans();
        try {
            $res = (new UserSpaceTrainService())->get_user_space_train($this->user_id);
            Db::commit();
        }  catch (Exception $e) {
            Db::rollback();
            return $this->fail("数据库异常");
        }
        return $this->json($res);
    }

    //出发
    public function start(): Response
    {
        $user_space_train_id = $this->request->post('user_space_train_id','0');
        // 启动事务
        Db::startTrans();
        try {
            $res = (new UserSpaceTrainService())->start($this->user_id,$user_space_train_id);
            Db::commit();
        }  catch (Exception $e) {
            Db::rollback();
            return $this->fail("数据库异常");
        }
        return $this->json($res);


    }



    //完成任务
    public function complete(): Response
    {

        $user_space_train_id = $this->request->post('user_space_train_id','0');
        // 启动事务
        Db::startTrans();
        try {
            $res = (new UserSpaceTrainService())->complete($this->user_id,$user_space_train_id);
            Db::commit();
        }  catch (Exception $e) {
            Db::rollback();
            return $this->fail("数据库异常");
        }
        return $this->json($res);

    }


}